﻿// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import store from './store'
// 服务部分
import {get, post, $delete, patch, workflowPost, basicWFGet, basicWFPost, basicWFDelete, basicWFPatch, httpAxios} from './axios/http.js'
// 公共的js方法
import MyPlugin from '../static/js/publicMethods.js'
import api from './axios/api.js'
import comFunc from './common_js/comFunc'

// 开发环境时，需要引入一个皮肤的样式文件(打包时，需要注释掉)
// import '../static/style/theme_dark/css/theme_dark.scss'
import '../static/style/theme_default/css/theme_default.scss'
// import '../static/style/theme_default_007cc2/css/theme_default_007cc2.scss'

// 字体图标
import './iconfont/iconfont.css'
import components from './components/DynamicComponents/index'

Vue.use(ElementUI, {size: 'medium'})
Vue.config.productionTip = false
Vue.prototype.$get = get
Vue.prototype.$basicWFGet = basicWFGet
Vue.prototype.$post = post
Vue.prototype.$basicWFPost = basicWFPost
Vue.prototype.$workflowPost = workflowPost
Vue.prototype.$delete = $delete
Vue.prototype.$basicWFDelete = basicWFDelete
Vue.prototype.$patch = patch
Vue.prototype.$basicWFPatch = basicWFPatch
Vue.prototype.$httpAxios = httpAxios
Vue.prototype.$api = api
Vue.use(MyPlugin)
Vue.use(components)

store.dispatch('setHttpEncrypt', false)
get('/_init').then(response => {
  console.debug('init')
  if (response.status === 200 && response.data) {
    let re = response.data.result
    if (re.data && re.nonce_str && re.sign && re.timestamp) {
      let timestamp = re.timestamp
      let nonce_str = re.nonce_str
      let sign = re.sign
      let str = re.data + '#' + nonce_str + '#$2#' + timestamp
      if (comFunc.getMD5String(str) !== sign) {
        // 校验不通过的时候，视为无效的api服务，此时开启加密，并设定sharekey为随机串
        store.dispatch('setHttpEncrypt', true)
        store.dispatch('setShareKey', comFunc.getNonceStr())
      } else {
        let key = re.data.substring(0, 32)
        let content = comFunc.decryptAES(re.data.substring(32), key)
        if (content === 'false') {
          store.dispatch('setHttpEncrypt', false)
        } else {
          store.dispatch('setHttpEncrypt', true)
          store.dispatch('setShareKey', content)
        }
      }
    } else {
      store.dispatch('setHttpEncrypt', false)
    }
  }
  startApp()
}).catch(() => {
  store.dispatch('setHttpEncrypt', false)
  startApp()
})
const startApp = function () {
  /* eslint-disable no-new */
  new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
  })
}
